
package com.huizhan.web.controller;
 
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import java.util.*;

import com.huizhan.web.dao.*;
import com.huizhan.web.model.*;

/**
 * 项目办证/门禁点api
 */
 
@Api(tags = "ProjectDoor",description = "项目办证/门禁点")
@RestController
public class ProjectDoorController {
    
    @Autowired
    private ProjectDoorDao dao;
   
    /**
     * 获取项目办证/门禁点集合
     * @return api结果
     */
    @ApiOperation(value = "获取项目办证/门禁点集合")
    @RequestMapping(value =  "/api/ProjectDoor/",method = {RequestMethod.GET})
    public ApiResult<List<ProjectDoor>> Get(){
         ApiResult<List<ProjectDoor>> result=new ApiResult<List<ProjectDoor>>();
        try 
        {
            List<ProjectDoor> res = dao.findAll();
            result.setData(res);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据项目ID获取项目办证/门禁点集合
     * @param id 项目办证/门禁点ID
     * @return api结果
     */
    @ApiOperation(value = "根据项目ID获取项目办证/门禁点集合")
    @RequestMapping(value =  "/api/ProjectDoor/Project/{projectId}",method = {RequestMethod.GET})
    public ApiResult<List< ProjectDoor>> Project(@PathVariable String projectId){
        ApiResult<List<ProjectDoor>> result=new ApiResult<List<ProjectDoor>>();
        try
        {
            List<ProjectDoor> model= dao.findByProjectId(projectId);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 根据ID获取项目办证/门禁点信息
     * @param id 项目办证/门禁点ID
     * @return api结果
     */
    @ApiOperation(value = "根据ID获取项目办证/门禁点信息")
    @RequestMapping(value =  "/api/ProjectDoor/{id}",method = {RequestMethod.GET})
    public ApiResult<ProjectDoor> Get(@PathVariable Long id){
        ApiResult<ProjectDoor> result=new ApiResult<ProjectDoor>();
        try
        {
            ProjectDoor model= dao.getOne(id);
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 修改项目办证/门禁点信息
     * @param id 项目办证/门禁点ID
     * @param model 项目办证/门禁点信息
     * @return api结果
     */
    @ApiOperation(value = "修改项目办证/门禁点信息")
    @RequestMapping(value =  "/api/ProjectDoor/{id}",method = {RequestMethod.PUT})
    public ApiResult<ProjectDoor> Edit(@PathVariable Long id,@RequestBody ProjectDoor model){
        ApiResult<ProjectDoor> result=new ApiResult<ProjectDoor>();
        try
        {
            ProjectDoor door=  dao.findByProjectIdAndName(model.getProjectId(),model.getName());

            if(door!=null&&door.getProjectDoorId()!=id){
                result.setHasError(true);
                result.setMessage("该名称已经存在");
            }else{
                model.setProjectDoorId(id);
                dao.save(model);
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return  result;
    }

    /**
     * 添加项目办证/门禁点信息
     * @param model 项目办证/门禁点信息
     * @return api结果
     */
    @ApiOperation(value = "添加项目办证/门禁点信息")
    @RequestMapping(value =  "/api/ProjectDoor/",method = {RequestMethod.POST})
    public ApiResult<ProjectDoor> Add(@RequestBody ProjectDoor model){
         ApiResult<ProjectDoor> result=new ApiResult<ProjectDoor>();
        try
        {
            if(dao.findByProjectIdAndName(model.getProjectId(),model.getName())==null){
                model.setCreateDate(new Date());
                dao.save(model);
            }else{
                result.setHasError(true);
                result.setMessage("该名称已经存在");
            }
            result.setData(model);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
    
    /**
     * 删除项目办证/门禁点信息
     * @param id 项目办证/门禁点ID
     * @return api结果
     */
    @ApiOperation(value = "删除项目办证/门禁点信息")
    @RequestMapping(value =  "/api/ProjectDoor/{id}",method = {RequestMethod.DELETE})
    public ApiResult<Object> Delete(@PathVariable  Long id){
        ApiResult<Object> result=new ApiResult<Object>();
        try 
        {
            dao.deleteById(id);
        }catch(Exception e){
            result.setHasError(true);
            result.setMessage(e.getMessage());
        }
        return result;
    }
}

